package com.data.modules.message.common;

import com.data.modules.ai.entity.AiRule;
import com.data.modules.ai.service.IAiRuleService;
import lombok.Getter;

import java.util.List;
import java.util.function.BiFunction;

/**
 * AI 消息类型
 */
@Getter
public enum AIMsgTypeEnum {

    /**
     * AI 消息类型
     */
    COUNTRY(1, "全国", (service, title) -> {
        return service.lambdaQuery()
                .select(AiRule::getTitle)
                .list();
    }),
    CITY(2, "城市", (service, title) -> {
        return service.lambdaQuery()
                .select(AiRule::getTitle)
                .eq(AiRule::getCity, title)
                .list();
    }),
    NAME_SPACE(3, "群名", (service, title) -> {
        return service.lambdaQuery()
                .select(AiRule::getTitle)
                .eq(AiRule::getTitle, title)
                .list();
    }),
    ;

    private Integer code;

    private String desc;

    private BiFunction<IAiRuleService, String, List<AiRule>> function;

    AIMsgTypeEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    AIMsgTypeEnum(Integer code, String desc, BiFunction<IAiRuleService, String, List<AiRule>> function) {
        this.code = code;
        this.desc = desc;
        this.function = function;
    }

    public static List<AiRule> getAiRuleByCity(String title, IAiRuleService service) {
        boolean contains = title.contains("-");
        //TODO 构建规则
        //1.群名（主体+城市，必须唯一） 公司城市 空格隔开
        return service.lambdaQuery()
                .select(AiRule::getTitle)
                //有主体 主体城市+主体全国
                .and(contains, wrapper -> {
                    String company = title.split("-")[0];
                    String city = title.split("-")[1];
                    wrapper.eq(AiRule::getTitle, title).or(wrapper1 -> {
                        wrapper1.eq(AiRule::getCompany, company).eq(AiRule::getCity, city);
                    });
                })
                //无主体 所有城市+主体全国
                .and(!contains, wrapper -> {
                    wrapper.in(AiRule::getCity, title, CITY);
                })
                .list();
    }


}
